package com.ruicar.afs.cloud.apply.contract.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.entity.ApplyGpsInfo;
import com.ruicar.afs.cloud.apply.common.entity.GpsProductInfo;
import com.ruicar.afs.cloud.apply.contract.condition.GpsCondition;
import com.ruicar.afs.cloud.apply.contract.mapper.ApplyGpsInfoMapper;
import com.ruicar.afs.cloud.apply.contract.service.ApplyGpsInfoService;
import com.ruicar.afs.cloud.apply.contract.service.GpsProductInfoService;
import com.ruicar.afs.cloud.apply.contract.vo.GpsInstallVO;
import com.ruicar.afs.cloud.bizcommon.gps.service.GpsOLEService;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysAddressParam;
import com.ruicar.afs.cloud.parameter.commom.service.TsysAddressParamService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author: Lee
 * @date: 2020/5/13 21:20
 * @description:
 */
@Service
@Slf4j
@AllArgsConstructor
public class ApplyGpsInfoServiceImpl extends ServiceImpl<ApplyGpsInfoMapper, ApplyGpsInfo> implements ApplyGpsInfoService {

    private GpsOLEService gpsOLEService;
    private final TsysAddressParamService service;
    private final GpsProductInfoService gpsProductInfoService;

    /**
     * 获取GPS安装前数据
     *
     * @param condition
     * @return
     */
    @Override
    public GpsInstallVO getGpsPreInstallInfo(GpsCondition condition) {
        return this.baseMapper.getGpsPreInstallInfo(condition);
    }

    /**
     * 派工单接口
     *
     * @param gpsInfo
     * @return
     */
    @Override
    public JSONObject addOrderWithVpsInfos(GpsCondition gpsInfo, GpsInstallVO installVO) {
        JSONObject json = new JSONObject();
        json.put("AppCode", gpsInfo.getCarId());
        //产品id根据规则获取供应商的id
        json.put("ProductId", gpsInfo.getGpsProductNo());
        json.put("DistrictCode", gpsInfo.getInstallCity());
        json.put("InstallAdd", gpsInfo.getInstallAddress());
        json.put("InstallTime", DateUtil.format(gpsInfo.getPreInstallTime(), "yyyy-MM-dd HH:mm:ss"));
        json.put("ShopName", installVO.getChannelName());
        json.put("LinkMan", gpsInfo.getSiteContact());
        json.put("LinkPhone", gpsInfo.getContactPhone());
        json.put("UserName", installVO.getCustName());
        json.put("UserPhone", installVO.getTelPhone());
        json.put("CarType", installVO.getStyleName());
        json.put("VIN", gpsInfo.getCarVin());
        json.put("UnitName", gpsInfo.getGpsSupplier());
        log.info("***GPS派单发送报文***   " + json);
        return gpsOLEService.addOrderWithVpsInfos(json);
    }

    /**
     * 改派工单接口
     *
     * @param gpsInfo
     * @return
     */
    @Override
    public JSONObject updateOrderWithVpsInfos(GpsCondition gpsInfo, GpsInstallVO installVO) {

        JSONObject json = new JSONObject();
        json.put("AppCode", gpsInfo.getCarId());
        //产品id根据规则获取供应商的id
        json.put("ProductId", "767e95b1-191e-406e-854d-790aecffd478");
        //根据省份value查询国标码
        TsysAddressParam tsysAddressParam = service.getProvinceOrCtiy(gpsInfo.getInstallCity());
        log.info("****************************国标码*******************   " + tsysAddressParam.getGbCode());
        json.put("DistrictCode", tsysAddressParam.getGbCode());
        json.put("InstallAdd", gpsInfo.getInstallAddress());
        json.put("InstallTime", DateUtil.format(gpsInfo.getPreInstallTime(), "yyyy-MM-dd HH:mm:ss"));
        json.put("ShopName", installVO.getDealerName());
        json.put("LinkMan", gpsInfo.getSiteContact());
        json.put("LinkPhone", gpsInfo.getContactPhone());
        json.put("UserName", installVO.getCustName());
        json.put("UserPhone", installVO.getTelPhone());
        json.put("CarType", installVO.getStyleName());
        json.put("VIN", gpsInfo.getCarVin());
        json.put("UnitName", gpsInfo.getGpsSupplier());
        log.info("***JSON***   " + json);
        return gpsOLEService.updateOrderWithVpsInfos(json);
    }

    /**
     * 撤销工单接口
     *
     * @param gpsInfo
     * @return
     */
    @Override
    public JSONObject cancelOrderWithVpsInfos(GpsCondition gpsInfo) {
        return gpsOLEService.cancelOrderWithVpsInfos(gpsInfo.getApplyNo());
    }

    /**
     * 工单综合查询
     *
     * @param applyNo
     * @return
     */
    @Override
    public JSONObject getAllOrderInfoByAppCode(String applyNo) {
        return gpsOLEService.getAllOrderInfoByAppCode(applyNo);
    }


}
